package com.zf.shuashua.ui.home;

import android.content.Intent;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.aliyun.apsara.alivclittlevideo.view.video.BaseVideoSourceModel;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.ui.repair.RepairActivity;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.BaseListData;
import com.zf.shuashua.bean.ConfigInfo;
import com.zf.shuashua.bean.VideoInfo;
import com.zf.shuashua.bean.VideoPointInfo;
import com.zf.shuashua.ui.mine.LogUtil;
import com.zf.shuashua.ui.mine.LoginActivity;
import com.zf.shuashua.utils.MD5;
import com.zf.shuashua.utils.ToastUtil;
import com.zf.shuashua.utils.Util;

public class HomeViewModel extends ViewModel {

    private MutableLiveData<String> mText;

    private MutableLiveData<BaseListData<VideoInfo>> mHomeData;
    private MutableLiveData<BaseData<VideoPointInfo>> mVideoPointInfo;
    private MutableLiveData<ConfigInfo> mConfigInfo;

    private int reopen = 1;

    public HomeViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("This is home fragment");
    }

    public LiveData<String> getText() {
        return mText;
    }

    public MutableLiveData<BaseListData<VideoInfo>> getHomeData() {
        if (mHomeData == null) {
            mHomeData = new MutableLiveData<>();
        }
        return mHomeData;
    }

    public void fetchHomeData(int page, int limit) {
        HttpData.getInstance().getHomeData(page, limit, new ACallback<BaseListData<VideoInfo>>() {
            @Override
            public void onSuccess(BaseListData<VideoInfo> data) {
                if (data.getCode() == 402 || data.getCode() == 406) {
                    ToastUtil.showToast(ShuaApplication.getContext(), data.getMessage() + "");
                    Intent intent = new Intent(ShuaApplication.getContext(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                    return;
                } else if (data.getCode() == 407) {
                    Intent intent = new Intent(ShuaApplication.getContext(), RepairActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                    return;
                }
                if (data.getData() != null && data.getData().size() > 0) {
                    getHomeData().setValue(data);
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                LogUtil.d("====" + errCode + "----------" + errMsg);

            }
        });
    }

    public MutableLiveData<BaseData<VideoPointInfo>> getVideoPointInfo() {
        if (mVideoPointInfo == null) {
            mVideoPointInfo = new MutableLiveData<>();
        }
        return mVideoPointInfo;
    }

    public void fetchVideoPointInfo(BaseVideoSourceModel currentModel) {
        int video_id = -1;
        long server_time = -1;
        if (currentModel != null) {
            video_id = currentModel.getId();
            server_time = currentModel.getServer_time();
        }
        if (video_id == -1) {
            return;
        }
        String sign = MD5.MD5Encode("time=" + server_time + "&key=" + Util.key).toUpperCase();
        HttpData.getInstance().getVideoPoint(video_id, server_time, sign, reopen, new AbsCallback<BaseData<VideoPointInfo>>() {

            @Override
            public void onRealSuccess(BaseData<VideoPointInfo> data) {
                getVideoPointInfo().setValue(data);
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                getVideoPointInfo().setValue(null);
            }
        });
        reopen = 0;
    }

    public MutableLiveData<ConfigInfo> getConfigInfo() {
        if (mConfigInfo == null) {
            mConfigInfo = new MutableLiveData<>();
        }
        return mConfigInfo;
    }

    public void fetchConfigInfo() {
        HttpData.getInstance().getConfigInfo(new AbsCallback<BaseData<ConfigInfo>>() {

            @Override
            public void onRealSuccess(BaseData<ConfigInfo> data) {
                getConfigInfo().setValue(data.getData());
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                getConfigInfo().setValue(null);
            }
        });
    }
}